package com.lengmomo.games.controller;

import com.lengmomo.games.service.IUserService;
import com.lengmomo.games.pojo.po.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController extends AbstractController {

    @Autowired
    private IUserService userService;

    /*跳转登录页*/
    @GetMapping("/login")
    public ModelAndView index() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String clientIp = request.getHeader("X-Real-IP");
//        System.out.println(clientIp);
        ModelAndView mv = new ModelAndView("login/login");
        return mv;
    }

    /*跳转注册页*/
    @GetMapping("/reg/toRegister")
    public ModelAndView toRegister() {
        return null;
//        ModelAndView mv = new ModelAndView("register/register");
//        return mv;
    }

    /**
     * 用户名重复校验
     *
     * @param name
     * @return
     */
    @RequestMapping("/reg/isFirstName")
    @ResponseBody
    public Boolean isFirstName(String name) {
        return userService.isFirstName(name);
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/reg/register")
    @ResponseBody
    public Boolean register(User user) {
        boolean result = userService.register(user);
        return result;
    }

    @PostMapping("/login")
    public ResponseEntity<ModelMap> login(User user) {
        ModelMap modelMap = new ModelMap();
        modelMap.put("flag", false);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(StringUtils.trim(user.getUsername()), StringUtils.trim(user.getPassword()));
        token.setRememberMe(false);//默认不记住密码

        try {
            subject.login(token);//shiro登录 realm执行认证方法
            modelMap.put("flag", true);
            modelMap.put("msg", "登陆成功");

        } catch (UnknownAccountException e) {
            modelMap.put("msg", "登录名不存在");//
        } catch (DisabledAccountException e) {
            modelMap.put("msg", "您的账户已经被冻结");
        } catch (IncorrectCredentialsException e) {
            modelMap.put("msg", "密码错误");//
        } catch (ExcessiveAttemptsException e) {
            modelMap.put("msg", "您错误的次数太多了吧,封你半小时");//
        } catch (RuntimeException e) {
            modelMap.put("msg", "运行异常");
        } finally {
            return ResponseEntity.ok(modelMap);
        }
    }

    @RequestMapping("/")
    public String toIndex1() {
        return "redirect:/index";
    }

    @RequestMapping("/index")
    public ModelAndView toIndex() {
        return new ModelAndView("index/index");
    }

    /**
     * 退出
     */
    @RequestMapping("/logout")
    @ResponseBody
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}
